package com.freesky.biz.icbc.service.impl;

import com.freesky.biz.icbc.domain.TEquityRecord;
import com.freesky.biz.icbc.mapper.EquityRecordMapper;
import com.freesky.biz.icbc.service.IEquityRecordService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class EquityRecordServiceImpl implements IEquityRecordService {

    @Autowired
    private EquityRecordMapper equityRecordMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TEquityRecord> findList(TEquityRecord equityRecord) {
        List<TEquityRecord> equityRecordList = equityRecordMapper.findList(equityRecord);
        for(int i=0;i<equityRecordList.size();i++){
            TEquityRecord record  = equityRecordList.get(i);
            if("新能源汽车分期".equalsIgnoreCase(record.getType())) {
                if ("1837319559741313024".equalsIgnoreCase(record.getActivityId())
                        || "1837319485166587904".equalsIgnoreCase(record.getActivityId())
                        || "1837319410336010240".equalsIgnoreCase(record.getActivityId())) {
                    record.setGrade(record.getGrade()+"(储蓄卡)");
                } else {
                    record.setGrade(record.getGrade()+"(信用卡)");
                }
                equityRecordList.set(i,record);
            }
        }
        return equityRecordList;
    }

    @Override
    public AjaxResult exportFile(TEquityRecord equityRecord) {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("EQUITY_RECORD");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(equityRecord, new TEquityRecord(), IEquityRecordService.class,
                "findList", "分期",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }
}
